﻿using System.Linq;
using System.Text;
using MRGSP.SurveyMaster.Core.Model;
using MRGSP.SurveyMaster.Core.Service.Utils;

namespace MRGSP.SurveyMaster.Service.Utils
{
    public class GridCellListCreateBuilder : IQuestionBuilder
    {
        public string Build(Question q)
        {
            var s = new StringBuilder();
            s.Append(
                string.Format(
                    @"<div id='qwrap{0}'><div id='util{0}' class='util'>{1}</div>", 
                    q.ColumnId, 
                    LinkBuilder.Build(LinkType.Remove, "deleteCellElement", q.ColumnId, q.Parent.Parent.ParentId.Value)));

            s.Append(HtmlGeneratorFactory.Get(q.Type).Generate(q));

            if (q.Children.Count() > 0)
            {
                s.Append("<ul>");
                var x = "";
                x = q.Children.Aggregate(x, (current, child) 
                                            => current + ("<li>" + QuestionCreateBuilder.Get(child.Type).Build(child) + "</li>"));
                s.Append(x);
                s.Append("</ul>");
            }
            s.Append("</div>");

            return s.ToString();
        }
    }
}